package com.lb.view;

import com.lb.kugou.MainActivity;

import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
public class MainContent extends LinearLayout {
	private PointF start;
	private static final int WIDTH_MOVE = 50;
	private static final int HEIGHT_MOVE = 40;
	private static final int RATE_MOVE = 500;
	private static int leftMin = 0;
	private static int currentSeat = 1;
	public MainContent(Context context, AttributeSet attrs) {
		super(context, attrs); 
		
	}
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		leftMin = MainActivity.menu_margin_width - MainActivity.screenWidth;
		int _left = 0;
		for(int i=0;i<getChildCount();i++){
			View _child = getChildAt(i);
			_child.setClickable(true);
			_child.layout(_left, t,_left+MainActivity.screenWidth , b);
			_left += MainActivity.screenWidth;
		}
		
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		for(int i=0;i<getChildCount();i++){
			View _child = getChildAt(i);
			_child.measure(widthMeasureSpec, heightMeasureSpec);
			
		}
		
	}

	private float x = 0;
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		float _x = ev.getRawX();
		float _y = ev.getRawY();
		switch(ev.getAction() & MotionEvent.ACTION_MASK){
		case MotionEvent.ACTION_DOWN:
			start = new PointF(_x, _y);
			x = _x;
			break;
		case MotionEvent.ACTION_MOVE:
			if(Math.abs(_x-start.x) >WIDTH_MOVE && Math.abs(_y-start.y)< HEIGHT_MOVE && ev.getEventTime()-ev.getDownTime() < RATE_MOVE ){
				return true;
			}
			break;
		case MotionEvent.ACTION_UP:	
			if(currentSeat == 0){
				setCurrentSeat(1);
				return true;
			}
			
			
			break;
		}
		return super.onInterceptTouchEvent(ev);
	}

	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float _x = event.getRawX();
		float _y = event.getRawY();
		switch(event.getAction()&MotionEvent.ACTION_MASK){
		case MotionEvent.ACTION_DOWN:
			start = new PointF(_x, _y);
			x = _x;
			break;
		case MotionEvent.ACTION_MOVE:
			/*if(Math.abs(_x-start.x) >WIDTH_MOVE && Math.abs(_y-start.y)< HEIGHT_MOVE && event.getEventTime()-event.getDownTime() < RATE_MOVE ){
				return true;
			}*/
			int _dx = Float.valueOf(start.x-_x).intValue();
			if(getScrollX() + _dx < leftMin){
				_dx = leftMin - getScrollX();
			}else if(getScrollX() + _dx > MainActivity.screenWidth){
				_dx = MainActivity.screenWidth  - getScrollX();
			}
			scrollBy(_dx, 0);
			start = new PointF(_x, _y);
			break;
		case MotionEvent.ACTION_UP:
			setCurrentSeat(x -_x);
			break;
		}
		if(currentSeat == 0){
			return false;
		}
		return true;
	}
	public void setCurrentSeat(float i){
		if(Math.abs(i) >= MainActivity.screenWidth/3){
			if(i>0 && currentSeat < 2){
				currentSeat = (currentSeat+1)%3;
			}else if(i<0 && currentSeat >0){
				currentSeat = (currentSeat-1)%3;
			}
		}
		notifySeat();
	}
	public void setCurrentSeat(int i){
		currentSeat = i;
		notifySeat();
	}
	public void notifySeat(){
		int scrollX = getScrollX();
		int dx = 0;
		switch(this.currentSeat%3){
		case 0 :
			dx = leftMin - scrollX;
			break;
		case 1:
			dx = 0 - scrollX;
			break;
		case 2:
			dx = MainActivity.screenWidth - scrollX;
			break;
		default: 
			this.currentSeat = 0;
			notifySeat();
		}
		scrollBy(dx, 0);
		
	}
	
	

}
